package com.dony.shop.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dony.common.utils.StringUtils;
import com.dony.shop.domain.GlobalModel;
import com.dony.shop.domain.ItemModel;
import com.dony.shop.mapper.ItemModelMapper;
import com.dony.shop.service.IGlobalModelService;
import com.dony.shop.service.IItemModelService;
import com.dony.shop.service.IItemService;
import com.dony.shop.util.ShopeeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 全球商品业务层处理
 *
 * @author sundw
 * @date 2022-03-08
 */
@Slf4j
@Service
public class ItemModelServiceImpl extends ServiceImpl<ItemModelMapper, ItemModel> implements IItemModelService {

    @Autowired
    ShopeeUtil shopeeUtil;
    @Resource
    IGlobalModelService globalModelService;
    @Resource
    IItemService itemService;

    /**
     * 查询并保存model数据
     *
     * @param itemId
     */
    @Override
    public void syncModel(Long shopId, Long itemId) {
        //查询所有model的数据
        this.remove(new QueryWrapper<ItemModel>().eq("item_id", itemId));
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("item_id", itemId);
        JSONObject response = shopeeUtil.shopGetHandler("/api/v2/product/get_model_list", shopId, paramMap);
        if (response == null) return;
        List<JSONObject> models = response.getList("model", JSONObject.class);
        List<ItemModel> shopModels = models.stream().map(model -> {
            ItemModel shopModel = new ItemModel();
            shopModel.setModelId(model.getLong("model_id"));
            shopModel.setItemId(itemId);
            shopModel.setModelName(model.getString("model_name"));
            shopModel.setPromotionId(model.getLong("promotion_id"));
            shopModel.setModelSku(model.getString("model_sku"));
            List<Integer> tierIndex = model.getList("tier_index", Integer.class);
            shopModel.setTierIndex(StringUtils.join(tierIndex, "-"));
            JSONObject priceInfo = (JSONObject) model.getJSONArray("price_info").get(0);
            shopModel.setCurrentPrice(priceInfo.getFloat("current_price"));
            shopModel.setOriginalPrice(priceInfo.getFloat("original_price"));
            shopModel.setInflatedPriceOfOriginalPrice(priceInfo.getFloat("inflated_price_of_original_price"));
            shopModel.setInflatedPriceOfCurrentPrice(priceInfo.getFloat("inflated_price_of_current_price"));
            shopModel.setCurrency(priceInfo.getString("currency"));
            return shopModel;
        }).collect(Collectors.toList());
        this.saveBatch(shopModels);
    }

    @Override
    public Float getGlobalModelPrice(Long itemId, String tierIndex) {
        Long globalProductId = itemService.getById(itemId).getGlobalProductId();
        GlobalModel model = globalModelService.getOne(new QueryWrapper<GlobalModel>().eq("global_item_id", globalProductId).eq("tier_index", tierIndex));
        return model.getOriginalPrice();
    }

}
